package chapter1.bagqueuestack.example;

import org.jetbrains.annotations.NotNull;

import java.util.Iterator;

/**
 * @author quanlinlin on 2018/12/1 23:43.
 * @version 1.0
 */
public class Bag<Item> implements Iterable<Item> {
    private Node first;


    @NotNull
    @Override
    public Iterator<Item> iterator() {
        return new ListIterator();
    }

    private class Node {
        Item item;
        Node next;
    }

    public void add(Item item) {
        Node oldfist = this.first;
        first = new Node();
        first.item = item;
        first.next = oldfist;
    }

    private class ListIterator implements Iterator<Item> {
        private Node current = first;

        @Override
        public boolean hasNext() {
            return current != null;
        }

        @Override
        public void remove() {

        }

        @Override
        public Item next() {
            Item item = current.item;
            current = current.next;
            return item;
        }

    }
}
